package com.cxhome.cxinfox.routing;

import java.util.Date;
import java.util.List;

import com.cxhome.cxinfox.QCellCore.PhoneHeadService;
import com.cxhome.cxinfox.QCellCore.ProvinceService;
import com.cxhome.cxinfox.customer.CustomerService;
import com.cxhome.cxinfox.productvas.ProductService;
import com.cxhome.cxinfox.provider.ProviderService;
import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.product.Product;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.StringUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 现代路由管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/moderrouting")
public class ModernRoutingController extends CxBaseController {

	private static final String path = "/pages/system/moderrouting/moderrouting_";

	public void index() {
		list();
	}

	public void list() {
		ModernRouting model = getModelByAttr(ModernRouting.class);

		StringBuilder orderSql = new StringBuilder("");

		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by cxinfo.createTime desc");
		} else {
			orderSql.append(" order by cxinfo.").append(orderBy.trim());
		}

		Page<ModernRouting> page = ModernRoutingService.service.paginate(model, getPaginator(), orderSql);

		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("providerId")));
		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}
//	public void list() {
//		ModernRouting model = getModelByAttr(ModernRouting.class);
//		
//		String mysql = "from ( SELECT " + "cmr1.*, " + "cp1.providerName providerName," + "cpv1.vasProdName vasPidName,"
//				+ "crp1.province as provinceName," + "cph1.cityName  ," + "cci1.customerName as customerName "
//				+ "from cx_modern_routing cmr1  "
//				+ "LEFT JOIN cx_customer_info cci1 on cmr1.customerId=cci1.customerId "
//				+ "LEFT JOIN cx_provider cp1 ON cmr1.provider =cp1.ids  "
//				+ "LEFT JOIN cx_product_vas cpv1 ON cmr1.vasPid = cpv1.ids  LEFT JOIN "
//				+ "cx_r_province crp1 ON crp1.headId = cmr1.province LEFT JOIN "
//				+ "(SELECT DISTINCT areaCode,cityName,province FROM cx_phone_head) cph1 ON cph1.areaCode = cmr1.numberAttribution ) cxinfo "
//				+ "where 1=1 ";
//		
//		SQLUtils sql = new SQLUtils(mysql);
//		
//		if (model.getAttrValues().length != 0) {
//			sql.whereEquals("status", model.getStr("status"));
//			sql.whereEquals("provider", model.getStr("provider"));
//			sql.whereEquals("customerId", model.getStr("customerId"));
//		}
//		
//		// 排序
//		String orderBy = getBaseForm().getOrderBy();
//		if (StringUtils.isEmpty(orderBy)) {
//			sql.append(" order by cxinfo.createTime desc");
//		} else {
//			sql.append(" order by cxinfo.").append(orderBy);
//		}
//		
//		Page<ModernRouting> page = ModernRouting.dao.paginate(getPaginator(), " select cxinfo.*  ",
//				sql.toString().toString());
//		
//		
//		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("providerId")));
//		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
//		setAttr("page", page);
//		setAttr("attr", model);
//		render(path + "list.html");
//	}

	/**
	 * 查看详情
	 */
	public void view() {
		String ids = getPara();
		ModernRouting model = ModernRoutingService.service.view(ids);

		setAttr("model", model);
		render(path + "view.html");
	}
	/**
	 * 跳转新增页面
	 */
	public void add() {
		ModernRouting model = getModel(ModernRouting.class);
		String productName = getPara("productName");
		// 查询所有平台产品
		List<Product> productlist = ProductService.service.findByParam(productName);

		setAttr("model", model);
		setAttr("page", productlist);
		setAttr("productName", productName);
		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("provider")));
		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
		setAttr("selectProvince", ProvinceService.service.selectProvince(model.getStr("province")));
		setAttr("selectArea",
				PhoneHeadService.service.selectArea(model.getStr("province"), model.getStr("numberAttribution")));
		render(path + "add.html");
	}

	/**
	 * 保存路由
	 */
	public void save() {

		ModernRouting model = getModel(ModernRouting.class);
		String productvasIds = getPara();
		String userid = getSessionUser().getUserName();
		ModernRoutingService.service.saveOrUpdate(model, productvasIds, userid);

		renderMessage("保存成功");
	}

	/**
	 * 删除当前数据
	 * 更新状态
	 */
	public void delete() {
		ModernRoutingService.service.delete(getPara(), getSessionUser().getUserName());
		list();

	}
}
